﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CKS.Core.Framework
{
    public static partial class Ext
    {
        public static bool IsS7Int16(this string parsetype)
        {
            parsetype = parsetype.ToLower();
            return parsetype.Equals("int") || parsetype.Equals("int16");
        }

        public static bool IsS7Real(this string parsetype)
        {
            parsetype = parsetype.ToLower();
            return parsetype.Equals("real") ;
        }

        public static bool IsS7String(this string parsetype)
        {
            parsetype = parsetype.ToLower();
            return parsetype.StartsWith("string[");
        }

        public static bool IsS7ArrayInt16(this string parsetype)
        {

            if (IsArrayType(parsetype, out int arraylenth, out string type2))
            {
                return type2.IsS7Int16();
            }
            return false;
        }

        public static bool IsArrayType(this string ArrayType, out int arrayLenth, out string ParseType)
        {
            ParseType = "";
            arrayLenth = 0;
            var dataType = ArrayType.ToLower();
            if (dataType.StartsWith("array[") && dataType.Contains("of"))
            {
                //Array[0..63] of Word

                var matches = Regex.Matches(dataType, "[0-9]+");
                if (matches.Count == 2)
                {
                    var Start = matches[0].Value.ToInt();
                    var End = matches[1].Value.ToInt();
                    double sizelenth = End - Start + 1;
                    arrayLenth = (int)sizelenth;
                    var arrayType = dataType.Split(' ')[2];
                    sizelenth = arrayType switch
                    {
                        "bool" => GetBoolLenth(sizelenth),
                        "int" => sizelenth * 2,
                        "word" => sizelenth * 2,
                        "dword" => sizelenth * 4, //uint
                        "dint" => sizelenth * 4,
                        "real" => sizelenth * 4,
                        "lreal" => sizelenth * 8,
                        _ => sizelenth
                    };
                    ParseType = arrayType;
                    //

                    //
                    return true;
                }
                else if (matches.Count == 3 && dataType.Contains("of string"))
                {
                    var arrayType = dataType.Split(' ')[2];
                    ParseType = arrayType;
                    var Start = matches[0].Value.ToInt();
                    var End = matches[1].Value.ToInt();
                    decimal sizelenth = End - Start + 1;
                    arrayLenth = (int)sizelenth;

                    return true;
                }
            }
            return false;
        }

        private static double GetBoolLenth(double sizelenth)
        {
            int d = (int)(sizelenth / 8);
            var yu = sizelenth % 8;
            if (yu == 0)
                return d;
            return (double)d + (yu / 10);
        }
    }
}
